    package android.pot.com.smartcar.util;

    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.pot.com.smartcar.R;
    import android.pot.com.smartcar.application.MyApplication;
    import android.widget.ImageView;

    import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
    import com.nostra13.universalimageloader.core.DisplayImageOptions;
    import com.nostra13.universalimageloader.core.ImageLoader;
    import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
    import com.nostra13.universalimageloader.core.assist.ImageScaleType;
    import com.nostra13.universalimageloader.core.assist.ImageSize;
    import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
    import com.nostra13.universalimageloader.core.display.BitmapDisplayer;
    import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
    import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

    /**
     * Created by zhaoshilu on 2017/8/10.
     */

    /**
     * 基于Universal imageLoader 封装的 图片加载工具类
     * 枚举形式 实现单例
     * Created by bailiangjin on 16/7/16.
     */
    public enum ImageLoadUtils implements com.lzy.imagepicker.loader.ImageLoader{
        INSTANCE;
        /**
         * 标准配置
         */
        private DisplayImageOptions normalOptions;
        /**
         * 圆形配置
         */
        private DisplayImageOptions circleOptions;
        /**
         * 圆角图片配置
         */
        private DisplayImageOptions roundedOptions;


        private BitmapDisplayer simpleBitmapDisplayer;
        private BitmapDisplayer circleBitmapDisplayer;
        private BitmapDisplayer roundedBitmapDisplayer;

        private int onLoadingImageResId;
        private int onEmptyImageResId;
        private int onFailedImageResId;


        /**
         * 构造方法 参数初始化 单例形式 只会初始化一次 避免不必要的资源开支
         */
        private ImageLoadUtils() {
            //初始化 全局默认图片
            onLoadingImageResId = R.drawable.shape_f5_bg;
            onEmptyImageResId = R.drawable.shape_f5_bg;
            onFailedImageResId = R.drawable.shape_f5_bg;

            simpleBitmapDisplayer = new SimpleBitmapDisplayer();
            normalOptions = getOption(onLoadingImageResId, onEmptyImageResId, onFailedImageResId, simpleBitmapDisplayer);

            //圆角图片 圆角半径dp
            int cornerRadiusDp = 10;
            //圆角大小通过 dp2px转换 使得 不同分辨率设备上呈现一致显示效果
            roundedBitmapDisplayer = new RoundedBitmapDisplayer(dip2px(MyApplication.getContext(),cornerRadiusDp));
            roundedOptions = getOption(onLoadingImageResId, onEmptyImageResId, onFailedImageResId, roundedBitmapDisplayer);


            circleBitmapDisplayer = new RoundedBitmapDisplayer(360);
            circleOptions = getOption(onLoadingImageResId, onEmptyImageResId, onFailedImageResId, circleBitmapDisplayer);
        }
        /**
         * 重构 抽取出的通用生成Option方法
         * @param onLoadingImageResId
         * @param onEmptyImageResId
         * @param onFailedImageResId
         * @param bitmapDisplayer normal 或圆形、圆角 bitmapDisplayer
         *
         * @return
         */
        private DisplayImageOptions getOption(int onLoadingImageResId, int onEmptyImageResId, int onFailedImageResId, BitmapDisplayer bitmapDisplayer) {
            DisplayImageOptions options = new DisplayImageOptions.Builder()
                    .showImageOnLoading(onLoadingImageResId)
                    .showImageForEmptyUri(onEmptyImageResId)
                    .showImageOnFail(onFailedImageResId)
                    .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
                    .cacheInMemory(false)
                    .cacheOnDisk(true)
                    .bitmapConfig(Bitmap.Config.RGB_565)
                    .considerExifParams(true)
                    .displayer(bitmapDisplayer)
                    .build();
            return options;
        }

        /**
         * 自定义 加载图片显示
         * @param imgResId
         * @return
         */
        private DisplayImageOptions getImgOption(int imgResId){

            return getOption(imgResId,imgResId,imgResId,simpleBitmapDisplayer);

        }

        /**
         * 自定义 加载圆形图片
         * @param imgResId
         * @return
         */
        private DisplayImageOptions getCircleOption(int imgResId){

            return getOption(imgResId,imgResId,imgResId,circleBitmapDisplayer);

        }




        public void loadImageView(ImageView iv, String url) {
            ImageLoader.getInstance().displayImage(url, iv, normalOptions);
        }

        public void loadCircleImageView(ImageView iv, String url,int resId) {
            DisplayImageOptions options = getCircleOption(resId);
            ImageLoader.getInstance().displayImage(url, iv,options );
        }

        public void loadRoundedImageView(ImageView iv, String url) {
            ImageLoader.getInstance().displayImage(url, iv, roundedOptions);
        }

        public void loadOptionImageView(ImageView iv, String url,int resId) {
            ImageLoader.getInstance().displayImage(url, iv, getImgOption(resId));
        }

        /**
         * dip px 转换工具类 将圆角进行转换 以实现不同分辨率设备上呈现相同效果
         * @param context
         * @param dpValue
         * @return
         */
        public static int dip2px(Context context, float dpValue) {
            final float density = context.getResources().getDisplayMetrics().density;
            return (int) (dpValue * density + 0.5f);
        }

        /**
         * 初始化方法
         * @param context
         */
        public void init(Context context) {
            // This configuration tuning is custom. You can tune every option, you may tune some of them,
            // or you can create default configuration by
            //  ImageLoaderConfiguration.createDefault(this);
            // method.
            ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
            config.threadPriority(Thread.NORM_PRIORITY);
            config.denyCacheImageMultipleSizesInMemory();
            config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
            config.memoryCacheExtraOptions(480, 800); // max width, max height，即保存的每个缓存文件的最大长宽
            config.diskCacheSize(80 * 1024 * 1024); // 80 MiB
            config.tasksProcessingOrder(QueueProcessingType.LIFO);
            config.writeDebugLogs(); // Remove for release app

            // Initialize ImageLoader with configuration.
            ImageLoader.getInstance().init(config.build());
        }

        @Override
        public void clearMemoryCache() {

        }

        @Override
        public void displayImagePreview(Activity activity, String path, ImageView imageView, int width, int height) {
            ImageSize size = new ImageSize(width, height);
            ImageLoadUtils.INSTANCE.loadImageView(imageView, "file://"+path);
        }

        @Override
        public void displayImage(Activity activity, String path, ImageView imageView, int width, int height) {
            ImageLoader.getInstance().displayImage("file://"+path, imageView, normalOptions);
        }
    }
